//
//  RHDevInfoModel.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/5/17.
//

#import "RHDevInfoModel.h"
@implementation PEDevInfoRowModel



@end
@implementation RHDevInfoModel

+ (NSDictionary *)modelContainerPropertyGenericClass {
    return @{@"rows" : [PEDevInfoRowModel class]};
 }
-(void)readDeviceInfo:(id)info
{
    //站点信息,电池信息  data返回的字典
    if([info isKindOfClass:[NSDictionary class]]){
        NSDictionary *dic = (NSDictionary*)info;
        [self.rows enumerateObjectsUsingBlock:^(PEDevInfoRowModel * _Nonnull rowModel, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if (dic[rowModel.key]) {
                if ([rowModel.dataType isEqualToString:@"int"]||[rowModel.dataType isEqualToString:@"float"]) {
//                    NSString *contentValue = [NSDecimalNumber decimalPlacesWithRoundUpWithDigit:1 value:[dic[rowModel.key] doubleValue]];
//                    rowModel.content = [NSString stringWithFormat:@"%@",contentValue];
                    NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithString:ValidNullDataToString(dic[rowModel.key], @"0")];
                    rowModel.content = [NSString stringWithFormat:@"%@",value];
                }else{
                    rowModel.content = ValidNullDataToString(dic[rowModel.key], @"--");
                }
                
            }
            
            //系统信息  结构不同
            if ([self.infoType isEqualToString:@"system"]) {
                if ([info[@"batInfo"] isKindOfClass:[NSDictionary class]]) {
                    NSDictionary *batinfoDic = info[@"batInfo"];
                    if (batinfoDic[rowModel.key]) {
                        if ([rowModel.dataType isEqualToString:@"int"]||[rowModel.dataType isEqualToString:@"float"]) {

                            NSDecimalNumber *value = [NSDecimalNumber decimalNumberWithDouble:[batinfoDic[rowModel.key] doubleValue]];
                            rowModel.content = [NSString stringWithFormat:@"%@",value];
                        }else{
                            rowModel.content = ValidNullDataToString(batinfoDic[rowModel.key], @"--");
                        }
                    }
                }
            }
        }];
        
       
        
        //再补上系统信息的  ems sn
        if ([self.infoType isEqualToString:@"system"]) {
            if ([dic[@"softInfo"] isKindOfClass:[NSArray class]]) {
                //软件信息
//                NSMutableDictionary *mtbDic = [NSMutableDictionary dictionary];
                NSMutableArray *softwareTempArr = [NSMutableArray array];
                NSArray *softArr = dic[@"softInfo"];
                NSMutableArray *tempArr = [NSMutableArray arrayWithArray:self.rows];
                [softArr enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull softDic, NSUInteger idx, BOOL * _Nonnull stop) {
                    __block BOOL isExit = NO;
                   
                        NSMutableDictionary *mtbDic = [NSMutableDictionary dictionary];
                        [mtbDic setObject:ValidNullData(softDic[@"endpointCode"]) forKey:@"endpointCode"];
                         [mtbDic setObject:ValidNullData(softDic[@"sn"]) forKey:@"sn"];
                         [mtbDic setObject:ValidNullData(softDic[@"version"]) forKey:@"softVersion"];
                        //找出对应的硬件版本
                        if ([dic[@"hardInfo"] isKindOfClass:[NSArray class]]){
                            NSArray *hardArr = dic[@"hardInfo"];
                            [hardArr enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull hardDic, NSUInteger idx, BOOL * _Nonnull stop) {
                                if ([mtbDic[@"sn"]isEqualToString:hardDic[@"sn"]]) {
                                    [mtbDic setObject:hardDic[@"version"] forKey:@"hardVersion"];
                                }
                            }];
                        }
                     
                    [softwareTempArr addObject:mtbDic];
                    
                }];
                //找出了所有软件版本和硬件版本
                [softwareTempArr enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull dic, NSUInteger idx, BOOL * _Nonnull stop) {
                     __block BOOL isExit = NO;
                    //判断是否存在,防止刷新时多次添加
                    [self.rows enumerateObjectsUsingBlock:^(PEDevInfoRowModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        if ([obj.content isEqualToString:dic[@"sn"]]) {
                            isExit = YES;
                            return;
                        }
                    }];
                    if (isExit) {
                        return;
                    }
                    PEDevInfoRowModel *snModel = [[PEDevInfoRowModel alloc]init];
                    snModel.title = ValidNullData(dic[@"endpointCode"]);
                    snModel.content = ValidNullData(dic[@"sn"]);
                    [tempArr addObject:snModel];
                    
                    
                    PEDevInfoRowModel *softModel = [[PEDevInfoRowModel alloc]init];
                    softModel.title   = @"软件版本";
                    softModel.content = ValidNullData(dic[@"softVersion"]);
                    [tempArr addObject:softModel];
                    
                    PEDevInfoRowModel *hardModel = [[PEDevInfoRowModel alloc]init];
                    hardModel.title   = @"硬件版本";
                    hardModel.content = ValidNullData(dic[@"hardVersion"]);
                    [tempArr addObject:hardModel];
                }];
                
                
                self.rows = tempArr.copy;
            }
        }
        
        //对相同的类型进行编号  PACK1  PACK2
        __block PEDevInfoRowModel *lastObj;
        __block NSInteger index = 1;
        [self.rows enumerateObjectsUsingBlock:^(PEDevInfoRowModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([lastObj.title isEqualToString:obj.title]) {
                lastObj.title = [NSString stringWithFormat:@"%@ %ld",lastObj.title,index];
                index += 1;
            }
            
            
            lastObj = obj;
        }];
    }
 
    
}
@end
